package com.zzi.Demo;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import com.itextpdf.text.pdf.BarcodePDF417;

public class BarCodeDemo {
	public static void text(String codeString, String filePath) throws Exception {
		BarcodePDF417 pdf = new BarcodePDF417();
		pdf.setText(codeString.getBytes("GBK"));
		pdf.setCodeColumns(9);
		pdf.setErrorLevel(3);
		Image pdfImg = pdf.createAwtImage(Color.black, Color.white);
		int offset = 5;
		System.out.println(pdfImg.getHeight(null) + " " + pdfImg.getWidth(null) + " " + offset);
		BufferedImage img = new BufferedImage(pdfImg.getWidth(null) + 2 * offset, pdfImg.getHeight(null) + 2 * offset, BufferedImage.TYPE_4BYTE_ABGR);
		Graphics g = img.getGraphics();
		g.drawImage(pdfImg, offset, offset, Color.white, null);
		OutputStream os = new BufferedOutputStream(new FileOutputStream(filePath));
		ImageIO.write(img, "PNG", os);
	}

	public static void main(String[] args) throws Exception {
		String content = "汉字示例AAAA-bbbb=1111!@#$%^&*(+)";
		String filePath = "g://barcode.png";
		text(content, filePath);
	}
}